package com.example.zhangxin.xutils3demo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

//这是一个练习的DEMO
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView iv;
    private Button btn, btn1, btn2;
    private DbManager db;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = x.getDb(((MyApplication) getApplicationContext()).getDaoConfig());
        initView();
        x.image().bind(iv, "http://resource.jufan.tv/jufan/userhead/20160716/fe1416bfa9eeefc2ef72.jpg");
        RequestParams params = new RequestParams("http://live.jufan.tv/cgi/user/getUserInfo?sign=E28BCDEEFA6A464DE114A0D119B69646D13DB95B&targetUid=500079723&r=lnai");

        x.http().get(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                Log.e("TAG", "onSuccess: " + result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    private void initView() {
        iv = (ImageView) findViewById(R.id.iv);
        btn = (Button) findViewById(R.id.btn);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:

                try {
                    List<LYJPerson> all = db.selector(LYJPerson.class).findAll();
                    for (int i = 0; i < all.size(); i++) {
                        System.out.println("LYJPerson" + i + ".name=" + all.get(i).getName() + "/////" + all.get(i).getAge());
                        System.out.println("LYJPerson" + i + ".name=" + all.get(i).getName() + "%%%%%" + all.get(i).getAge());
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn1:
                LYJPerson person1 = new LYJPerson();
                person1.setName("zhangyunlong");
                person1.setAge("23");
                LYJPerson person2 = new LYJPerson();
                person2.setName("chexxisdf");
                person2.setAge("34");
                Log.e("TAG", "onClick: R.id.btn1");
                try {
                    db.save(person1);
                    db.save(person2);
                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn2:
                Intent intent = new Intent(this, BitmapActivity.class);
                startActivity(intent);
                break;
        }

    }
}
